# python中操作mysql数据库：安装工具包，pip install pymysql

from pymysql import Connection
def get_db_connection():
    """获取数据库连接（复用此函数创建连接）"""
    try:
        connect = Connection(
            host='172.27.40.240',
            port=3306,
            database='test',
            user='sunce',
            password='123456',
            charset='utf8'
        )
        print("数据库连接成功")
        return connect
    except Exception as e:
        print(f"数据库连接失败: {e}")
        return None

def close_db_connection(connect):
    """关闭数据库连接"""
    if connect and not connect._closed:
        connect.close()
        print("数据库连接已关闭")